package com.bawei.mdm2207alast.file

import android.app.ProgressDialog
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.model.FileUploadEntity
import com.blankj.utilcode.util.ToastUtils
import com.king.ultraswiperefresh.indicator.CircularProgressIndicator
import kotlinx.coroutines.launch
import java.io.File

/**
 * 作者：赵亮
 * 时间：2024/12/16 15:34
 * 功能：本地文件
 */
@Composable
fun LocalFilesPage(
    navController: NavController = rememberNavController(),
    viewModel: FileViewModel = hiltViewModel()
) {

    // 1. 数据
    val list = remember {
        SnapshotStateList<File>()
    }
    // 选中文件名字
    var chName by remember {
        mutableStateOf("")
    }
    // 选中文件路径
    var chPath by remember {
        mutableStateOf("")
    }
    // 上传进度对话框
    var isShow by remember {
        mutableStateOf(false)
    }
    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {
                        ToastUtils.showShort(it.msg)
                    }

                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        when (it.data) {
                            is Collection<*> -> {
                                list.clear()
                                list.addAll(it.data as Collection<File>)
                            }

                            is FileUploadEntity -> {
                                ToastUtils.showShort(it.data.msg)
                                isShow = false
                            }
                        }
                    }
                }
            }
        }
        launch {
            viewModel.sendIntent(FileIntent.ScanFile(File("/sdcard/Download")))
        }
    }

    Column {
        // 标题
        Box(modifier = Modifier.fillMaxWidth()) {
            BaseTitle(navController = navController, title = "本地文件选择")
            Text(
                text = "上传", modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(top = 24.dp, end = 15.dp)
                    .clickable {
                        isShow = true

                        // 发送上传文件的意图
                        viewModel.sendIntent(FileIntent.FileUpload(chName, chPath))
                    }
            )
            if (isShow) {
                AlertDialog(onDismissRequest = { },
                    confirmButton = { },
                    title = { Text(text = "上传中...") },
                    text = { LinearProgressIndicator(trackColor = Color.Blue, color = Color.Red) }
                )
            }
        }

        // 2. 展示
        LazyColumn(
            verticalArrangement = Arrangement.spacedBy(10.dp),
            modifier = Modifier.padding(start = 15.dp)
        ) {
            items(list) {
                Row(modifier = Modifier.clickable {
                   // viewModel.sendIntent(FileIntent.ScanFile(File(it.absolutePath)))
                    chName = it.name
                    chPath = it.path
                }) {
                    Box {
                        Image(
                            modifier = Modifier.size(40.dp),
                            painter = painterResource(id = R.drawable.icon3),
                            contentDescription = null
                        )
                        Image(
                            modifier = Modifier
                                .size(10.dp)
                                .offset(x = 6.dp)
                                .align(Alignment.TopEnd),
                            painter = painterResource(id = if (chName == it.name) R.drawable.ic_action_check else R.drawable.ic_action_uncheck),
                            contentDescription = null
                        )
                    }
                    Column(modifier = Modifier.padding(start = 10.dp)) {
                        Text(text = it.name, fontSize = 12.sp)
                        Spacer(modifier = Modifier.height(10.dp))
                        Text(text = it.absolutePath, fontSize = 12.sp)
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun LocalFilesPagePreview() {
    LocalFilesPage()
}

